package com.mtw.bbs.reply.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mtw.bbs.reply.job.ReplyAsyncJob;
import com.mtw.bbs.reply.pojo.entity.ReplyIndex;
import com.mtw.bbs.reply.pojo.entity.ReplySubject;
import com.mtw.bbs.reply.pojo.form.ReplyForm;
import com.mtw.bbs.reply.pojo.vo.ReplyAdminVo;
import com.mtw.bbs.reply.service.ReplyIndexService;
import com.mtw.bbs.reply.service.ReplySubjectService;
import com.mtw.bbs.common.core.constant.SysConstant;
import com.mtw.bbs.common.core.vo.Result;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;


/**
 * 评论管理
 *
 */
@RestController
@RequestMapping("comment/admin")
@RequiredArgsConstructor
public class ReplyAdminController {

    private final ReplyAsyncJob replyAsyncJob;
    private final ReplyIndexService replyIndexService;
    private final ReplySubjectService replySubjectService;


    /**
     * 分页获取评论
     * @param page              当前页
     * @param size              分页大小
     * @param content           内容
     * @param subjectId         评论区id
     * @return                  评论
     */
    @GetMapping("/list")
    public Result getVoPage(@RequestParam(name = "page") int page,
                            @RequestParam(name = "size") int size,
                            @RequestParam(name = "content", required = false) String content,
                            @RequestParam(name = "subjectId", required = false) String subjectId) {


        QueryWrapper<ReplyIndex> qw = new QueryWrapper<>();
        qw.like(StringUtils.isNotBlank(content),"r.content",content);
        qw.eq(StringUtils.isNotBlank(subjectId),"r.subject_id",subjectId);
        qw.orderByDesc("r.created_date");

        Page<ReplyAdminVo> voPage = replyIndexService.getReplyAdminVoPage(page, size, qw);

        return Result.success(voPage);
    }



    /**
     * 启用评论
     */
    @PutMapping("/enable/{id}")
    public Result enableReply(@PathVariable String id) {
        return updateStatus(id,SysConstant.DEFAULT_ENABLE);
    }



    /**
     * 禁用评论
     */
    @PutMapping("/disable/{id}")
    public Result disableReply(@PathVariable String id) {

        return updateStatus(id,SysConstant.DEFAULT_DISABLE);
    }


    /**
     * 更新评论状态
     * @param id        评论id
     * @param status    状态
     */
    private Result updateStatus(String id,Integer status){

        ReplyIndex entity = replyIndexService.getById(id);
        if (entity == null){
            return Result.fail("更新失败");
        }
        if (entity.getStatus().equals(status)){
            return Result.success();
        }
        boolean result = replyIndexService.updateReplyStatus(id, status);
        if (result){
            ReplySubject subject = replySubjectService.getById(entity.getSubjectId());
            if (subject != null){
                ReplyForm form = new ReplyForm();
                form.setId(id);
                form.setParentId(entity.getParentId());
                form.setRootId(entity.getRootId());
                form.setObjectId(subject.getObjectId());
                form.setObjectType(subject.getObjectType());
                replyAsyncJob.updateReplyCountByStatus(form,SysConstant.DEFAULT_ENABLE == status);
            }

        }
        return result?Result.success():Result.fail("更新失败");

    }




    /**
     * 删除评论
     * @param id        评论id
     */
    @DeleteMapping("/delete/{id}")
    public Result deleteReply(@PathVariable String id) {

        ReplyIndex entity = replyIndexService.getById(id);
        if (entity == null){
            return Result.fail("删除失败");
        }
        boolean result = replyIndexService.deleteReply(id);
        if (result && entity.getStatus().equals(SysConstant.DEFAULT_ENABLE)){
            ReplySubject subject = replySubjectService.getById(entity.getSubjectId());
            if (subject != null){
                ReplyForm form = new ReplyForm();
                form.setId(id);
                form.setParentId(entity.getParentId());
                form.setRootId(entity.getRootId());
                form.setObjectId(subject.getObjectId());
                form.setObjectType(subject.getObjectType());
                replyAsyncJob.updateReplyCountByDelete(form);
            }
        }
        return result?Result.success():Result.fail("删除失败");
    }



}
